package com.rainner.admin.action;

import java.sql.SQLException;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Actions;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;

import com.rainner.admin.action.base.EasyuiAction;
import com.rainner.admin.defined.PermissionDefined;
import com.rainner.admin.defined.SystemDefine;
import com.rainner.admin.javabean.ReaderBean;
import com.rainner.admin.server.ReaderServer;
import com.rainner.admin.server.RolePermissionServer;

@Results({ @Result(name = "READERMANAGE", type = "freemarker", location = "/WEB-INF/content/reader/index.ftl") })
public class ReaderAction extends EasyuiAction {
	private static final long serialVersionUID = 1L;

	private final String base = "/reader/manage";
	private String type;
	private ReaderServer readerServer = new ReaderServer();
	private ReaderBean readerBean = new ReaderBean();
	private RolePermissionServer permissionServer = new RolePermissionServer();

	@Actions({ @Action(base), @Action(base + "/{type}") })
	public String readerManage() throws SQLException {
		Object username = ServletActionContext.getRequest().getSession().getAttribute("username");
		if (type == null &&  username != null) {
			return "READERMANAGE";
		}else if(username ==null){
			return null;
		}
		if (SystemDefine.READER_INIT.equals(type) && permissionServer.checkPermission(PermissionDefined.SEARCH_READER, username.toString())) {
			map.put("total", readerServer.countReaderData());
			map.put("rows", readerServer.allReaderData(page, rows));
		} else if (SystemDefine.READER_DELETE.equals(type) && permissionServer.checkPermission(PermissionDefined.DELETE_READER, username.toString())) {
			if(readerServer.delReaderData(readerBean)){
				this.result.setMsg("删除读者操作成功.");
			}else{
				this.result.setMsg("删除读者操作失败.");
			}
			map.put("message", this.result);
		}
		return MAP;
	}

	public void setType(String type) {
		this.type = type;
	}

	public void setReaderBean(ReaderBean readerBean) {
		this.readerBean = readerBean;
	}

}
